package com.wondertek.poms.core.service.impl;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.common.utils.StringUtil;
import com.wondertek.poms.core.service.IFirstProductInfoPackageService;
import com.wondertek.poms.dao.po.FirstProductInfoPackage;
import com.wondertek.poms.dao.repo.IFirstProductInfoPackageDao;
import com.wondertek.poms.dao.repo.ISystemConfigDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenjijiang
 * @description
 */
@Service
@Scope("prototype")
public class FirstProductInfoPackageServiceImpl extends ServiceImpl<FirstProductInfoPackage, Long> implements IFirstProductInfoPackageService {
	@Autowired
	private IFirstProductInfoPackageDao firstProductInfoPackageDao;
	@Autowired
	private ISystemConfigDao systemConfigDao;
	@PersistenceContext
    public EntityManager entityManager;
	/**
	 * 逻辑删除，需要自己实现
	 * @param id id
	 * @return 删除掉的一级产品
	 */
	@Override
	public FirstProductInfoPackage deleteLogic(Long id) {
		return null;
	}

	/**
	 * 更新一级产品
	 * @param model  需更新的一级产品
	 * @return 更新后的一级产品
	 */
	@Override
	public FirstProductInfoPackage merge(FirstProductInfoPackage model) {
		return firstProductInfoPackageDao.save(model);
	}


	@Override
	public List<FirstProductInfoPackage> findPomsAll() {
			//生产上是11
			String firstProductIds=systemConfigDao.findByCkey("FIRSTPRODUCT_BESIDES_IDS")==null?"":systemConfigDao.findByCkey("FIRSTPRODUCT_BESIDES_IDS").getValue();
			StringBuilder condition = new StringBuilder();
			condition.append("select fp.first_product_id,fp.first_product_name from first_product_info_package fp ");
			if(StringUtils.isNotBlank(firstProductIds)){
				condition.append(" where first_product_id not in (").append(firstProductIds).append(") ");
			}
			Query query=entityManager.createNativeQuery(condition.toString());
			List<Object[]> list=query.getResultList();
			List<FirstProductInfoPackage> firstPrdInfoList=new ArrayList<FirstProductInfoPackage>();
			for (Object[] obj:list){
				FirstProductInfoPackage firstPrdInfo=new FirstProductInfoPackage();
				firstPrdInfo.setFirstProductId(StringUtil.nullToLong(obj[0]));
				firstPrdInfo.setFirstProductName(StringUtil.nullToString(obj[1]));
				firstPrdInfoList.add(firstPrdInfo);
			}
			return firstPrdInfoList;
		}
}
